#include "GraphicsTool.h"

void CGraphicsTool::DrawImage( CBitmapContext& aGc,const CFbsBitmap* aImage,const CFbsBitmap* aImgMask, 
		TInt aDestX, TInt aDestY, TUint aWidth , TUint aHeight, TInt aSrcX, TInt aSrcY) 
{
 	if (aImage)
 	{
		TInt width=aWidth;
		TInt height=aHeight;
		
		if(width==0)
		{
			width=aImage->SizeInPixels().iWidth;
		}
		if(height==0)
		{
			height=aImage->SizeInPixels().iHeight;
		}
		
		TRect  srcRect( TPoint( aSrcX, aSrcY), TSize( width, height ) );
 	
		if(aImgMask)
			aGc.BitBltMasked(TPoint(aDestX,aDestY), aImage, srcRect, aImgMask, EFalse );
 		else
 			aGc.BitBlt(TPoint(aDestX,aDestY), aImage,srcRect);
 	}
}

void CGraphicsTool::DrawImage( CBitmapContext& aGc, const CFbsBitmap* aImage,const CFbsBitmap* aImgMask, 
				const TRect& aDestRect,
				CGraphicsContext::TTextAlign aHAlign/*=CGraphicsContext::ELeft*/,
				TInt aHMargin/*=0*/,
				CGraphicsContext::TTextAlign aVAlign/*=CGraphicsContext::ECenter*/)
	{
		if(aImage)
		{
			TInt width=0;
			TInt height=0;
			width=aImage->SizeInPixels().iWidth;
			height=aImage->SizeInPixels().iHeight;
			
			if(width>aDestRect.Width())
				width=aDestRect.Width();
			
			if(height>aDestRect.Height())
				height=aDestRect.Height();
			
			TInt destX=0;
			TInt destY=0;
			
			switch(aVAlign)
			{
			case CGraphicsContext::ELeft:
				{
					destY=aDestRect.iTl.iY;
				}
				break;
			case CGraphicsContext::ECenter:
				{
					destY=aDestRect.iTl.iY+(aDestRect.Height()-height)/2;
				}
				break;
			case CGraphicsContext::ERight:
				{
					destY=aDestRect.iBr.iY-height;
				}
				break;
			}
			
			switch(aHAlign)
			{
			case CGraphicsContext::ELeft:
				{
					destX=aDestRect.iTl.iX+aHMargin;
				}
				break;
			case CGraphicsContext::ECenter:
				{
					destX=aDestRect.iTl.iX+(aDestRect.Width()-width)/2;
				}
				break;
			case CGraphicsContext::ERight:
				{
					destX=aDestRect.iBr.iX-aHMargin-width;
				}
				break;
			}
			
			DrawImage(aGc,aImage,aImgMask,destX,destY,width,height,0,0);	
		}
	}

void CGraphicsTool::DrawImage( CBitmapContext& aGc, const CFbsBitmap* aImage,const CFbsBitmap* aImgMask, 
                const TRect& aDestRect,TInt aDrawImageMode)
{
    switch(aDrawImageMode)
    {
        case ETile:
            {
                if(!aImage)
                    return;
                
                TInt imgWidth=aImage->SizeInPixels().iWidth;
                TInt imgHeight=aImage->SizeInPixels().iHeight;
                
                if(imgWidth==0 || imgHeight==0)
                    return;
                
                for(TInt y=aDestRect.iTl.iY; y<aDestRect.iBr.iY; y+=imgHeight)
                {
					TInt height=imgHeight;
					if(y+height>aDestRect.iBr.iY)
						height=aDestRect.iBr.iY-y;
                    for(TInt x=aDestRect.iTl.iX; x<aDestRect.iBr.iX; x+=imgWidth)
                    {
						TInt width=imgWidth;
						if(x+width>aDestRect.iBr.iX)
							width=aDestRect.iBr.iX-x;
                        DrawImage(aGc,aImage,aImgMask,TRect(TPoint(x,y),TSize(width,height)),
                        		CGraphicsContext::ELeft,0,CGraphicsContext::ELeft);
                    }
                }
            }
            break;
        case EScale:
            {
                if (aImage)
                {
                    if(aImgMask)
                    {
                        TRect srcRect;
                        srcRect.SetSize(aImage->SizeInPixels());
                        
                        aGc.DrawBitmapMasked(aDestRect,aImage,srcRect,aImgMask,ETrue);
                    }
                    else
                    {
                        aGc.DrawBitmap(aDestRect,aImage);
                    }
                }
            }
            break;
    }
}

void CGraphicsTool::DrawText( CBitmapContext& aGc, 
		const TDesC&  aText,  TInt  aDestX, TInt aDestY,  
		const TRgb& aFontColor, const CFont* aFont)
{
	if(aFont)
		aGc.UseFont(aFont);
	
	aGc.SetPenColor(aFontColor);
	aGc.DrawText(aText, TPoint(aDestX, aDestY));
	
	if(aFont)
		aGc.DiscardFont();
}

void CGraphicsTool::DrawText( CBitmapContext& aGc, 
            const TDesC& aText, TInt aDestLeftX, TInt aDestBottomY,  
            TInt aFontColor, const CFont* aFont)
{
    TRgb fontColor(aFontColor,255);
    DrawText( aGc,  aText, aDestLeftX, aDestBottomY, fontColor, aFont);
}

void CGraphicsTool::DrawText( CBitmapContext& aGc, 
		const TDesC&  aText, const TRect& aRect,
		const TRgb& aFontColor,const CFont* aFont,
		CGraphicsContext::TTextAlign aAlign,TInt aMargin)
{
	if(!aFont)
		return;
	
	aGc.UseFont(aFont);
	DrawText(aGc, aText,aRect,aFontColor,aFont->HeightInPixels(),aAlign,aMargin);
	aGc.DiscardFont();
}

void CGraphicsTool::DrawText( CBitmapContext& aGc, 
            const TDesC&  aText, const TRect& aRect,
            TInt aFontColor,const CFont* aFont,
            CGraphicsContext::TTextAlign aAlign,TInt aMargin)
{
    TRgb fontColor(aFontColor,255);
    DrawText(aGc, aText, aRect,fontColor,aFont,aAlign,aMargin);
}

void CGraphicsTool::DrawText( CBitmapContext& aGc, 
		const TDesC&  aText, const TRect& aRect,
		const TRgb& aFontColor,TInt aFontHeight,
		CGraphicsContext::TTextAlign aAlign,TInt aMargin)
	{
		aGc.SetPenColor(aFontColor);
		TInt baselineOffset=(aRect.Height()+aFontHeight)/2;		
		aGc.DrawText(aText,aRect,baselineOffset,aAlign,aMargin);
	}

void CGraphicsTool::DrawText( CBitmapContext& aGc, 
            const TDesC& aText, const TRect& aRect,
            TInt aFontColor,TInt aFontHeight,
            CGraphicsContext::TTextAlign aAlign,TInt aMargin)
{
    TRgb fontColor(aFontColor,255);
    DrawText(aGc, aText, aRect,fontColor,aFontHeight,aAlign,aMargin);
}

void CGraphicsTool::DrawRect( CBitmapContext& aGc, const TRect& aRect,const TRgb& aBorderColor, 
        TBool aUseBrush,const TRgb& aBrushColor) 
{
    aGc.SetPenColor( aBorderColor );
    aGc.SetPenStyle( CGraphicsContext::ESolidPen );
        
    if ( aUseBrush )
    {
        aGc.SetBrushColor( aBrushColor );
        aGc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    }
    
    aGc.DrawRect( aRect );
    
    aGc.SetPenStyle( CGraphicsContext::ENullPen );
    aGc.SetBrushStyle( CGraphicsContext::ENullBrush );
}

void CGraphicsTool::DrawRect( CBitmapContext& aGc, const TRect& aRect , TUint32 aBorderColor, 
                    TBool aUseBrush,TUint32 aBrushColor,TInt aBrushAlpha)
{
    TRgb borderRgb(aBorderColor,aBrushAlpha);
    TRgb brushRgb(aBrushColor,aBrushAlpha);
    DrawRect(aGc,aRect,borderRgb,aUseBrush,brushRgb);
}

void CGraphicsTool::DrawLine( CBitmapContext& aGc, TPoint aBegin , TPoint aEnd, TInt aColor,TInt aAlpha)
{
    TRgb penRgb(aColor,aAlpha);
    DrawLine(aGc,aBegin,aEnd,penRgb);
}

void CGraphicsTool::DrawLine( CBitmapContext& aGc, TPoint aBegin , TPoint aEnd, const TRgb& aColor)
{
    aGc.SetPenColor( aColor );
    aGc.SetPenStyle( CGraphicsContext::ESolidPen );
    aGc.DrawLine(aBegin, aEnd);
}
